package com.invengo.base.excel;

import android.os.Handler;
import android.util.Log;

import com.blankj.utilcode.util.TimeUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.utils.constant.Constants;

public class ImportExcelUtils {
    private static String NOW_DATE = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));

    public static final String Suffix = ".xls";

    private ImportExcelUtils() {
    }

    private static ImportExcelUtils instance;


    public static ImportExcelUtils getInstance() {
        if (instance == null) {
            synchronized (ImportExcelUtils.class) {
                if (instance == null) {
                    instance = new ImportExcelUtils();
                }
            }
        }
        return instance;
    }

//    public List<String> getSearchBook(String path) {
//        if (!new File(path).exists()) {
//            return null;
//        }
//        return ExcelUtils.readExcel(path);
//    }

    /**
     * 读取表格
     *
     * @param
     * @return
     */
    public static List<String> readExcelForSearch(InputStream inputStream) {
        String defaultPath = Constants.IMPORT.IMPORT_PATH + File.separator + Constants.IMPORT.IMPORT_SEARCH_ADD_NAME;
        try {
            //默认路径
            if (inputStream == null) {
                inputStream = new FileInputStream(defaultPath);
            }
            Workbook book = Workbook.getWorkbook(inputStream);
            book.getNumberOfSheets();
            List<String> keywords = new ArrayList<>();
            Sheet sheet = book.getSheet(0);
            //标题行
            Cell[] headerRow = sheet.getRow(0);
            int columnIndex = -1;
            Cell headerCell;
            for (int i = 0; i < headerRow.length; i++) {
                headerCell = headerRow[i];
                if (headerCell != null &&
                        "图书编码".equalsIgnoreCase(
                                headerCell.getContents().trim())) {
                    columnIndex = i;
                    break;
                }
            }
            if (columnIndex == -1) {
                new Handler().postDelayed(() -> ToastUtils.showShort("图书编码列不存在"), 1500);
                return keywords;
            }

            int rows = sheet.getRows();
            for (int i = 1; i < rows; i++) {
                Cell cell = sheet.getCell(columnIndex, i);
                String contents = cell.getContents();
                if (contents != null && !contents.isEmpty())
                    keywords.add(contents);
            }
            book.close();
            return keywords;
        } catch (Exception e) {
            Log.e("Exception", e + "");
            return null;
        }
    }

    public boolean fileExists() {
        String path = Constants.IMPORT.IMPORT_PATH + File.separator + Constants.IMPORT.IMPORT_SEARCH_ADD_NAME;
        return new File(path).exists();
    }
}
